/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * Projects
 * Copyright (C) Arnaud Duval 2010 <arnaud.duval@gmail.com>
 * 
 * Projects is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Projects is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <lap-history.hpp>

///@brief Default constructor
LapHistory::LapHistory()
{
    
}

///@brief Copy constructor
///@param l LapHistory object to copy
LapHistory::LapHistory(const LapHistory& l)
{
    trackPoints = l.trackPoints;
}

///@brief Destructor
LapHistory::~LapHistory()
{
    
}

///@brief Overoad of = operator
///@param l LapHistory object to copy
LapHistory& LapHistory::operator=(const LapHistory& l)
{
    trackPoints = l.trackPoints;
    return (*this);
}

///@brief Read lap history from a XML node in a TCX file
///@param lapNode Pointer to Xml node to read
void LapHistory::ReadFromTcxFile(xmlpp::Node* lapNode)
{
    if(lapNode->get_name() == "Lap")
    {
        // Parsing lap node children
        xmlpp::Node::NodeList lapNodeChildren = lapNode->get_children();
        for(xmlpp::Node::NodeList::iterator iterLapNodeChildren = lapNodeChildren.begin();
            iterLapNodeChildren != lapNodeChildren.end();
        ++iterLapNodeChildren)
            {
                xmlpp::Node* lapNodeChild = dynamic_cast<xmlpp::Node*>(*iterLapNodeChildren);
                if(lapNodeChild->get_name() == "Track")
                {
                    ParseTrackNodeFromTcxFile(lapNodeChild);
                }
            }
    }
}

///@brief Parse a XML node corrseponding to a track in a TCX file
///@param trackNode XML node to parse
void LapHistory::ParseTrackNodeFromTcxFile(xmlpp::Node* trackNode)
{
    if(trackNode->get_name() == "Track")
    {
        //Parsing track node childen
        xmlpp::Node::NodeList trackNodeChildren = trackNode->get_children();
        for (xmlpp::Node::NodeList::iterator iterTrackNodeChildren = trackNodeChildren.begin();
             iterTrackNodeChildren != trackNodeChildren.end();
             ++iterTrackNodeChildren)
             {
                 xmlpp::Node* trackNodeChild = dynamic_cast<xmlpp::Node*>(*iterTrackNodeChildren);
                 if(trackNodeChild->get_name() == "Trackpoint")
                 {
                     RichTrackPoint newPoint;
                     newPoint.ReadFromTcxFile(trackNodeChild)                ;
                     trackPoints.push_back(newPoint);
                 }
             }
    }
}

///@brief Write to a file containing the history
///@param fout output stream to file
void LapHistory::WriteXML(std::ofstream& fout)
{
    fout << "    <Track>\n";
    for(std::vector<RichTrackPoint>::iterator iter = trackPoints.begin();
        iter != trackPoints.end(); ++iter)
        {
            fout << "     <TrackPoint>\n";
            (*iter).WriteXML(fout);
            fout << "     </TrackPoint>\n";
        }
    fout << "    </Track>\n";
}

///@brief Append recorded DateTime entries to a given list
///@param list DateTime list to complete
void LapHistory::AppendToDateTimeList(std::list<DateTime> & list)
{
    for (std::vector<RichTrackPoint>::iterator iter = trackPoints.begin();
         iter != trackPoints.end() ; ++iter)
         {
             list.push_back((*iter).GetDateTime());
         }
}

///@brief Read from a file containing the history
///@param node Pointer to XML node to read
void LapHistory::ReadHistoryFile(xmlpp::Node* node)
{
    if(node->get_name() == "Lap")
    {
        xmlpp::Node::NodeList lapNodeChildren = node->get_children();
        for(xmlpp::Node::NodeList::iterator iterLapNodeChildren = lapNodeChildren.begin();
            iterLapNodeChildren != lapNodeChildren.end();
            ++iterLapNodeChildren)
        {
            xmlpp::Node* lapNodeChild = dynamic_cast<xmlpp::Node*>(*iterLapNodeChildren);
            if(lapNodeChild->get_name() == "Track")
            {
                xmlpp::Node::NodeList trackNodeChildren = lapNodeChild->get_children();
                for(xmlpp::Node::NodeList::iterator iterTrackNodeChildren = trackNodeChildren.begin() ;
                    iterTrackNodeChildren != trackNodeChildren.end() ;
                    ++iterTrackNodeChildren)
                {
                    xmlpp::Node* trackNodeChild = dynamic_cast<xmlpp::Node*>(*iterTrackNodeChildren);
                    if(trackNodeChild->get_name() == "TrackPoint")
                    {
                        RichTrackPoint newPoint;
                        newPoint.ReadHistoryFile(trackNodeChild);
                        trackPoints.push_back(newPoint);
                    }
                }
            }
        }
    }
}

///@brief Return the track
std::vector<RichTrackPoint> LapHistory::GetTrack()
{
    return trackPoints;
}

///@brief Return the number of points
unsigned int LapHistory::GetNumberOfPoints()
{
    return trackPoints.size();
}

